package com.battery.service.impl;

import com.battery.dao.OrderDao;
import com.battery.pojo.Order;
import com.battery.service.OrderService;
import com.battery.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService{
    @Autowired(required=false)

    private OrderDao orderDao;
    @Override
    public Result addOrder(Order order) {
        try {
            if(orderDao.insertOrder(order)>0)
            {
                return Result.ok().message("生成租借订单成功");
            }
            else{
                return Result.error().message("生成租借订单失败");
            }
        }catch (Exception e){
            return Result.error().message("生成租借订单失败");
        }



    }

    @Override
    public Result dropOrder(int order_id) {
        try {
            if(orderDao.deleteOrder(order_id)>0)
            {
                return Result.ok().message("删除租借订单成功");
            }
            else{
                return Result.error().message("删除租借订单失败");
            }
        }catch (Exception e){
            return Result.error().message("删除租借订单失败");
        }

    }

    @Override
    public Result queryOrder(int order_id) {
        try {
            Order order=orderDao.selectOrder(order_id);
            if(order!=null)
            {
                return Result.ok().message("查找租借订单成功").data("order",order);
            }
            else{
                return Result.error().message("查找租借订单失败");
            }
        }catch (Exception e){
            return Result.error().message("查找租借订单失败");
        }

    }

    @Override
    public Result modifyOrder(Order order) {
        try{
            if (orderDao.updateOrder(order)>0){
                return Result.ok().message("修改成功");
            }
            else return Result.error().message("修改失败：未找到对应记录");
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("修改失败：已经存在对应记录");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("修改失败："+e.toString());
        }

    }

    @Override
    public Result queryAllOrder() {
        try {
            List<Order> orders=orderDao.getAllOrder();
            if(orders.size()>0)
            {
                return Result.ok().message("查找所有租借订单成功").data("orders",orders);
            }
            else{
                return Result.error().message("查找所有租借订单失败");
            }
        }catch (Exception e){
            return Result.error().message("查找所有租借订单失败");
        }

    }

    @Override
    public Result queryAllMerOrder(int mer_id) {
        try {
            List<Order> orders=orderDao.getAllMerOrder(mer_id);
            if(orders.size()>0)
            {
                return Result.ok().message("查找商户租借订单成功").data("orders",orders);
            }
            else{
                return Result.error().message("查找商户租借订单失败");
            }
        }catch (Exception e){
            return Result.error().message("查找商户租借订单失败");
        }
    }

    @Override
    public Result queryAllUserOrder(int user_id) {
        try {
            List<Order> orders=orderDao.getAllUserOrder(user_id);
            if(orders.size()>0)
            {
                return Result.ok().message("查找商户租借订单成功").data("orders",orders);
            }
            else{
                return Result.error().message("查找商户租借订单失败");
            }
        }catch (Exception e){
            return Result.error().message("查找商户租借订单失败");
        }
    }

    @Override
    public Order calculatedOrder(int id, Timestamp endTime){
        System.out.println("计算价格");
        Order or = orderDao.selectOrder(id);
        Timestamp startime = or.getOrder_create_time();
        int lastTime = (int)(endTime.getTime()-startime.getTime())/(1000*60);
        int device_id = or.getDevice_id();
        DeviceServiceImpl dsi = new DeviceServiceImpl();
        Result device = dsi.getDeviceById(device_id);
        System.out.println(device);
        String rule_id = (String) device.getData().get("rule_id");
        System.out.println(rule_id);
        int new_rule_id = Integer.parseInt(rule_id);
        RuleServiceImpl rsi = new RuleServiceImpl();
        Result rule = rsi.findRule(new_rule_id);
        double unit_price = (double) rule.getData().get("unit_price");
        int free_time = (int)(Timestamp.valueOf(rule.getData().get("free_time").toString()).getTime()/(1000*60));
        free_time=30;
        double unit_time = (double)(Timestamp.valueOf(rule.getData().get("unit_time").toString()).getTime()/(1000*60));
        unit_time = 30;
        double upper_limit = (double) rule.getData().get("upper_limit");
        double price;

        if(lastTime>free_time){
            double minusTime = (double)lastTime - free_time;
            int multi = (int)Math.round(minusTime/unit_time);
            price = multi*unit_price;
            if (price>upper_limit)
                price = upper_limit;
            or.setPrice(price);
            orderDao.updateOrder(or);
            return or;
        }
        else{
            or.setPrice(0);
            orderDao.updateOrder(or);
            return or;
        }

    }
}
